package com.cooper.controllers.item;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.constants.DictConstants;
import com.cooper.constants.ResourceConstants;
import com.cooper.controllers.Controller;
import com.cooper.core.JsonResult;
import com.cooper.entity.DesignerEntity;
import com.cooper.entity.ItemInfo;
import com.cooper.pagination.PageInfo;
import com.cooper.service.admin.AddressService;
import com.cooper.service.admin.MallCategoryService;
import com.cooper.service.core.DictService;
import com.cooper.service.item.ItemImageService;
import com.cooper.service.item.ItemRepository;
import com.cooper.service.item.ItemSkuService;
import com.cooper.service.seller.DesignerService;
import com.cooper.vo.AddressVo;
import com.cooper.vo.ItemInfoVo;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.rest.Get;

public class DetailController extends Controller {
	
	@Autowired
	private ItemRepository itemRepository;
	
	@Autowired
	private MallCategoryService mallCategoryService;
	
	@Autowired
	private AddressService addressService;
	
	@Autowired
	private ItemImageService itemImageService;
	
	@Autowired
	private ItemSkuService itemSkuService;
	
	@Autowired
	private DictService dictService;
	
	@Autowired
	private DesignerService designerService;
	
	@Get
	public String execute(Invocation inv, @Param("id") long itemId) throws Exception{
		inv.addModel("deletedMap", this.dictService.makeOptionList(DictConstants.DELETED, "", ""));
		ItemInfo itemInfo = this.itemRepository.loadById(itemId);
		inv.addModel("itemInfo", itemInfo);
		if(null != itemInfo){
			inv.addModel("categoryName", this.mallCategoryService.findName(itemInfo.getCid()));
			inv.addModel("itemImages", this.itemImageService.getItemImageListByItemId(itemId));
			
			inv.addModel("itemSkuList", this.itemSkuService.getItemSkuListByItemId(itemId));
			
			AddressVo originAddress = this.addressService.getAddressVoByOriginAndOuterId(ResourceConstants.AddressOrigin.ITEM_ORIGIN, itemId);
			inv.addModel("originAddress", originAddress);
			
			DesignerEntity designer = this.designerService.findDesignerByUserId(itemInfo.getSellerId());
			AddressVo designerAddress = this.addressService.getAddressVoByOriginAndOuterId(ResourceConstants.AddressOrigin.SELLER_ADDRESS, itemInfo.getSellerId());
			inv.addModel("designerAddress", designerAddress);
			inv.addModel("designer", designer);
			
			List<ItemInfoVo> relatedItemList = this.itemRepository.getRelatedItemList(itemInfo.getSellerId(), itemId);
			inv.addModel("relatedItemList", relatedItemList);
		}
		
		return "ItemDetail";
	}
	
	@Get("somethingLike")
	public Object somethingLike(Invocation inv, @Param("itemId") long itemId, PageInfo pageInfo) throws Exception {
		JsonResult json = new JsonResult();
		ItemInfo itemInfo = this.itemRepository.loadById(itemId);
		pageInfo.setPageSize(4);
		List<ItemInfoVo> itemList = this.itemRepository.querySomethingLike(itemInfo.getCid(), itemInfo.getSellerId(), pageInfo);
		json.put("itemList", itemList);
		json.setSuccess(true);
		return json;
	}

}
